<template>
  <el-dialog v-model="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false"
    :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="rules" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()"
      label-width="120px">
      <el-row>
        <el-col :span="24">
          <el-form-item label="任务名称" prop="tskName">
            <el-input v-model="dataForm.tskName" placeholder="请输入"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <!--任务负责人-->
      <el-row>
        <el-col :span="24">
          <el-form-item label="任务负责人">
            <el-checkbox @change="handleCheckAllChange">全选</el-checkbox>
          </el-form-item>
        </el-col>
        <el-form-item style="margin-top: -10px;">
          <el-checkbox-group v-model="dataForm.tskStufsList">
            <el-checkbox v-for="leader in taskleader" :key="leader.userId" :label="leader.userId" :value="leader.userId">{{leader.realName }}
            </el-checkbox>
          </el-checkbox-group>
        </el-form-item>
      </el-row>
      <!--工作人员-->
      <el-row>
        <el-col :span="24">
          <el-form-item label="工作人员">
            <el-checkbox @change="handleCheckAllChange2">全选</el-checkbox>
          </el-form-item>
        </el-col>
        <el-form-item style="margin-top: -10px;">
          <el-checkbox-group v-model="dataForm.tskPrincipalsList">
            <el-checkbox v-for="leader in tskWork" :key="leader.userId" :label="leader.userId" :value="leader.userId">{{leader.realName }}
            </el-checkbox>
          </el-checkbox-group>
        </el-form-item>
      </el-row>
      <!-- 备注 -->
      <el-row>
        <el-col :span="24">
          <el-form-item label="备注">
            <el-input v-model="dataForm.tskRemark" placeholder="请输入" style="height: 50px"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 保存 取消 -->
      <el-form-item style="margin-top: 50px;">
        <el-button type="primary" @click="dataFormSubmitHandle()">保存</el-button>
        <el-button @click="visible = false">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script lang="ts" setup>
import { reactive, ref, } from "vue";
import baseService from "@/service/baseService";
import { useI18n } from "vue-i18n";
import { ElMessage } from "element-plus";
import { da } from "element-plus/es/locale";
const { t } = useI18n();
const emit = defineEmits(["refreshDataList"]);
const visible = ref(false);
const dataFormRef = ref();

const dataForm = reactive({
  id: "",
  tskName: "",
  tskRemark: "",
  tskStufsList: [] as number[],
  tskPrincipalsList: [] as number[],
  typeList: [1, 6, 7],
  exhId: "",
  
});

//校验
const rules = ref({
  tskName: [
    {required: true,message: '必填项',trigger: 'change',},
  ],
});

//初始化
const init = (id: number, curExhId:string) => {
  
  visible.value = true;
  dataForm.id = "";
  dataForm.exhId = curExhId
  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields();
  }
  //获取多选框数据
  getTaskleader();
  getTaskWork();

  if (id) {
    getEdit(id)
  }
};


// //编辑
const getEdit = (id: number) => {
  baseService.get("/crm/crmtask/" + id).then((res) => {
    Object.assign(dataForm, res.data);
  });
};


//任务负责人
const taskleader = ref([] as any[]);

/**
 * 任务负责人-全选
 * @param val
 */
const handleCheckAllChange = (val: boolean) => {
  // 选中
  if (val) {
    for (let i = 0; i < taskleader.value.length; i++) {
      dataForm.tskStufsList.push(taskleader.value[i].userId);
    }
  } else {
    dataForm.tskStufsList = [];
  }
}

/**
 * 获取任务负责人
 */
const getTaskleader = () => {
  // 1,6 7:领导 组长 数据组
  return baseService.get("/sys/user/getLeaderList", { typeList: dataForm.typeList.join() }).then((res) => {
    if (res.code !== 0) {
      return ElMessage.error(res.msg);
    }
    taskleader.value = res.data;
  });
};


//工作人员
const tskWork = ref([] as any[]);

/**
 * 工作人员-全选
 * @param val
 */
const handleCheckAllChange2 = (val: boolean) => {
  // 选中
  if (val) {
    for (let i = 0; i < tskWork.value.length; i++) {
      dataForm.tskPrincipalsList.push(tskWork.value[i].userId);
    }
  } else {
    dataForm.tskPrincipalsList = [];
  }
}

/**
 * 获取工作人员
 */
const getTaskWork = () => {
  // 4：工作人员
  return baseService.get("/sys/user/usersByType/4").then((res) => {
    if (res.code !== 0) {
      return ElMessage.error(res.msg);
    }
    tskWork.value = res.data;
  });
};

// 表单提交
const dataFormSubmitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false;
    }
    dataForm.tskPrincipalsList = dataForm.tskPrincipalsList.map((item: number) => Number(item));
    dataForm.tskStufsList = dataForm.tskStufsList.map((item: number) => Number(item));
    (!dataForm.id ? baseService.post : baseService.put)("/crm/crmtask", {tskName:dataForm.tskName,tskStufsList:dataForm.tskStufsList,tskPrincipalsList:dataForm.tskPrincipalsList,tskRemark:dataForm.tskRemark,exhId: Number(dataForm.exhId)}).then((res) => {
      ElMessage.success({
        message: t("prompt.success"),
        duration: 500,
        onClose: () => {
          visible.value = false;
          emit("refreshDataList");
        }
      });
    });
  });
};

defineExpose({
  init
});
</script>

<style lang="scss" scoped>
.el-checkbox {
  width: 12%;
}
</style>